﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.Roster.TroopRosterElement
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System.Collections.Generic;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.ObjectSystem;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.Roster
{
  public struct TroopRosterElement : ISerializableObject, ISavedStruct
  {
    private static readonly TroopRosterElement StaticInvalidElement = new TroopRosterElement((CharacterObject) null);
    [SaveableField(0)]
    private int _number;
    [SaveableField(1)]
    private int _woundedNumber;
    [SaveableField(2)]
    private int _xp;
    [SaveableField(4)]
    public CharacterObject Character;

    public static void AutoGeneratedStaticCollectObjectsTroopRosterElement(
      object o,
      List<object> collectedObjects)
    {
      ((TroopRosterElement) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    private void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      collectedObjects.Add((object) this.Character);
    }

    internal static object AutoGeneratedGetMemberValueDeltaXp(object o)
    {
      return (object) ((TroopRosterElement) o).DeltaXp;
    }

    internal static object AutoGeneratedGetMemberValueCharacter(object o)
    {
      return (object) ((TroopRosterElement) o).Character;
    }

    internal static object AutoGeneratedGetMemberValue_number(object o)
    {
      return (object) ((TroopRosterElement) o)._number;
    }

    internal static object AutoGeneratedGetMemberValue_woundedNumber(object o)
    {
      return (object) ((TroopRosterElement) o)._woundedNumber;
    }

    internal static object AutoGeneratedGetMemberValue_xp(object o)
    {
      return (object) ((TroopRosterElement) o)._xp;
    }

    public static TroopRosterElement Invalid => TroopRosterElement.StaticInvalidElement;

    public int Number
    {
      get => this._number;
      set
      {
        this._number = value >= 0 ? value : throw new MBUnderFlowException("ItemRosterElement::Character");
      }
    }

    public int WoundedNumber
    {
      get
      {
        if (!this.Character.IsHero)
          return this._woundedNumber;
        return !this.Character.HeroObject.IsWounded ? 0 : 1;
      }
      set
      {
        this._woundedNumber = value >= 0 ? value : throw new MBUnderFlowException("ItemRosterElement::WoundedNumber");
      }
    }

    public int Xp
    {
      get => this._xp;
      set
      {
        if (value < 0)
        {
          Debug.FailedAssert("false", "C:\\Develop\\MB3\\Source\\Bannerlord\\TaleWorlds.CampaignSystem\\Roster\\TroopRosterElement.cs", nameof (Xp), 77);
          this._xp = 0;
        }
        else
          this._xp = value;
      }
    }

    [SaveableProperty(5)]
    public int DeltaXp { get; internal set; }

    public TroopRosterElement(CharacterObject character)
    {
      this.Character = character;
      this._number = 0;
      this._woundedNumber = 0;
      this._xp = 0;
      this.DeltaXp = 0;
    }

    internal void Clear()
    {
      this.Character = (CharacterObject) null;
      this._number = 0;
    }

    void ISerializableObject.SerializeTo(IWriter writer)
    {
      writer.WriteUInt(this.Character != null ? this.Character.Id.InternalValue : 0U);
      writer.WriteInt(this._number);
      writer.WriteInt(this._woundedNumber);
      writer.WriteInt(this._xp);
    }

    void ISerializableObject.DeserializeFrom(IReader reader)
    {
      uint id = reader.ReadUInt();
      this.Character = (CharacterObject) null;
      if (id != 0U)
        this.Character = MBObjectManager.Instance.GetObject(new MBGUID(id)) as CharacterObject;
      this._number = reader.ReadInt();
      this._woundedNumber = reader.ReadInt();
    }

    public override string ToString() => this.Number.ToString() + " " + this.Character?.ToString();

    public override bool Equals(object obj)
    {
      return obj is TroopRosterElement other && this.Equals(other);
    }

    public bool Equals(TroopRosterElement other) => this.Character == other.Character;

    public override int GetHashCode() => this.Character == null ? 0 : this.Character.GetHashCode();

    bool ISavedStruct.IsDefault()
    {
      return this.Character == null && this._number == 0 && this._woundedNumber == 0 && this._xp == 0 && this.DeltaXp == 0;
    }
  }
}
